import 'package:flutter/material.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../common/bg_page.dart';
import '../../tool/assets.dart';
import 'sound_item.dart';

class MyTime extends DelayPageDelegate {
  MyTime({super.key});

  @override
  void keyboardHidden() {
    super.keyboardHidden();
    var ch = globalModel.ch_list.firstWhere(($0) => $0.isDelayEdit, orElse: () => default_ch);
    globalController.endDelayEdit(ch);
  }

  Widget rowSound(GlobalModel global, List<int> list) {
    return Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: list.map(($0) => SoundItem(ch: global.ch_list[$0])).toList());
  }

  List<Widget> delayTypes() {
    return [
      {'INCH': const Color(0xFF95CDE1)},
      {'CM': const Color(0xFF89B855)},
      {'MS': const Color(0xFF8495BC)}
    ].asMap().entries.map(($0) {
      return Transform.scale(
        scale: globalController.rxModel.value.delay_type == $0.key + 1 ? 1.2 : 1,
        child: Align(
          alignment: Alignment.center,
          child: myTextButton(
              width: 80,
              height: 30,
              radius: 4,
              color: $0.value.values.first,
              colors: [$0.value.values.first, Colors.white.withOpacity(0.2)],
              child: Center(child: Text($0.value.keys.first, style: font20)),
              onTap: () => globalController.modify((item) => item.delay_type = $0.key + 1)),
        ),
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height, // 关键！
      child: Obx(() {
        var global = globalController.rxModel.value;
        var typeList = delayTypes();
        return SizedBox.expand(
          child: Stack(
            children: [
              Positioned.fill(child: BgPage(bg: assets.B6, logo: assets.L6)),

              Positioned.fill(
                  child: Padding(
                padding: const EdgeInsets.all(0),
                child: Row(
                  children: [
                    ///左边
                    Flexible(
                      flex: 4,
                      child: Padding(
                          padding: const EdgeInsets.only(top: 10, left: 10, bottom: 60),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              myTextButton(
                                  width: 120,
                                  height: 35,
                                  radius: 4,
                                  color: const Color(0xFF88B855),
                                  colors: [const Color(0xFF88B855), Colors.white.withOpacity(0.2)],
                                  child: Center(child: Text('DELAY', style: font30)),
                                  onTap: () {}),
                              typeList[0],
                              typeList[1],
                              typeList[2],
                              Container(
                                width: 140,
                                height: 50,
                                decoration: boxDecoration(5),
                                child:
                                    Center(child: Text('TIME ALIGNMENT', textAlign: TextAlign.center, style: font20)),
                              ),
                            ],
                          )),
                    ),
                    // Flexible(flex: 1, child: Container()),

                    ///中间
                    Flexible(
                      flex: 2,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Flexible(child: SoundItem(ch: global.ch_list[6])),
                          Expanded(child: Container()),
                          Flexible(child: SoundItem(ch: global.ch_list[7]))
                        ],
                      ),
                    ),

                    ///右边
                    Flexible(
                      flex: 9,
                      child: Column(
                        children: [
                          Flexible(flex: 1, child: rowSound(global, [4, 2, 0])),
                          Flexible(
                              flex: 2, child: Align(alignment: Alignment.centerLeft, child: Image.asset(assets.car))),
                          Flexible(flex: 1, child: rowSound(global, [5, 3, 1]))
                        ],
                      ),
                    ),

                    // Flexible(flex: 1, child: Container()),
                  ],
                ),
              )),

              ///退出按钮
              Positioned(left: 10, bottom: 10, child: backButtom(context))
            ],
          ),
        );
      }),
    );
  }
}
